package com.lianliankan.llkan;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import com.example.llkan.R;
import com.lianliankan.model.Score;
import com.lianliankan.model.Users;
import com.lianliankan.util.ButtonSelector;
import com.lianliankan.util.ExtendMethod;
import com.lianliankan.util.SettlementAsyncTask;

public class SettlementActivity extends Activity {
	private ImageButton backButton;
	private ImageButton againButton;
	private TextView sltScore;
	private TextView sltMoney;
	private TextView sltExperience;
	private final int BASEEXP = 40;
	private ExtendMethod extendMethod;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_settlement);
		findView();
		init();
	}

	private void findView() {
		sltScore = (TextView) findViewById(R.id.sltScore);
		sltMoney = (TextView) findViewById(R.id.sltMoney);
		sltExperience = (TextView) findViewById(R.id.sltExperience);
		backButton = (ImageButton) findViewById(R.id.backButton);
		againButton = (ImageButton) findViewById(R.id.againButton);
	}

	private void init() {
		Score score = new Score();
		Users user = new Users();
		ButtonSelector bSelector = new ButtonSelector();

		Bundle extras = getIntent().getExtras();
		score.setPlayScore(extras.getInt("score"));
		int money = (int) (score.getPlayScore() / 10000 * 0.4);
		int experience = 0;
		if (score.getPlayScore() >= 100000) {
			experience = (score.getPlayScore() / 100000) * 10 + BASEEXP;
		} else {
			experience = score.getPlayScore() > 10000 ? BASEEXP : (score
					.getPlayScore() > 5000 ? 30 : 0);
		}
		if (score.getPlayScore() > 10000 && money < 1) {
			money = 1;
		}
		user.setMoney(money);
		user.setExperience(experience);
		score.setUser(user);
		sltScore.setText(String.valueOf(score.getPlayScore()));
		sltMoney.setText(String.valueOf(money));
		sltExperience.setText(String.valueOf(experience));

		SettlementAsyncTask settlementAsyncTask = new SettlementAsyncTask(
				score, "end");
		settlementAsyncTask.execute();

		backButton.setBackgroundDrawable(bSelector.newSelector(
				SettlementActivity.this, R.drawable.backbtn,
				R.drawable.backpress, R.drawable.backpress,
				R.drawable.backpress));
		backButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// extendMethod.buttonClickSound(SettlementActivity.this);
				Intent intent = new Intent(SettlementActivity.this,
						RankActivity.class);
				startActivity(intent);
				finish();
			}
		});

		againButton.setBackgroundDrawable(bSelector.newSelector(
				SettlementActivity.this, R.drawable.againbtn,
				R.drawable.againpress, R.drawable.againpress,
				R.drawable.againpress));
		againButton.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				// extendMethod.buttonClickSound(SettlementActivity.this);
				Intent intent = new Intent(SettlementActivity.this,
						StagePropertyActivity.class);
				startActivity(intent);
				finish();
			}
		});
	}

	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			Intent intent = new Intent(SettlementActivity.this,
					RankActivity.class);
			startActivity(intent);
			finish();
		}
		return false;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.welcome, menu);
		return true;
	}

	protected void onResume() {
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onResume();
	}
}
